package com.house.web.controller.system;


import com.house.common.core.controller.BaseController;
import com.house.common.core.domain.AjaxResult;
import com.house.common.core.domain.entity.House;
import com.house.system.domain.HouseSupport;
import com.house.system.service.IHouseService;
import com.house.system.service.IHouseSupportService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.house.system.domain.RentalType;
import com.house.system.service.IRentalTypeService;

import java.util.List;

/**
 * 租售类型Controller
 *
 * @author tc
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/domain/rental/type")
public class RentalTypeController extends BaseController {
    @Autowired
    private IRentalTypeService rentalTypeService;

    @Autowired
    private IHouseService houseService;

    @Autowired
    private IHouseSupportService houseSupportService;


    /**
     * 获取租售类型信息
     */
    @PreAuthorize("@ss.hasPermi('domain:type:query')")
    @GetMapping("/info/{houseId}")
    public AjaxResult getTypeInfo(@PathVariable("houseId") Long houseId) {
        AjaxResult ajax = AjaxResult.success();
        //查询相关的房屋信息
        House house = houseService.selectHouseByHouseId(houseId);
        ajax.put("house", house);
        //查询租售类型信息
        List<RentalType> rentalTypes = rentalTypeService.selectRentalTypeByHouseId(houseId);
        ajax.put("rentalTypes", rentalTypes);
        //查询房屋配套家具
        List<HouseSupport> supports = houseSupportService.selectSupportsByHouseId(houseId);
        ajax.put("supports", supports);
        return ajax;
    }

    /**
     * 获取租售类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('domain:type:query')")
    @GetMapping(value = "/{houseId}")
    public AjaxResult getInfo(@PathVariable("houseId") Long houseId) {
        AjaxResult ajax = AjaxResult.success();
        //以集合的形式返回,但是集合内只会有一个元素
        RentalType rentalType = rentalTypeService.selectRentalTypeByHouseId(houseId).get(0);
        ajax.put("rentalType", rentalType);
        return ajax;
    }

    /**
     * 新增租售类型
     */
    @PreAuthorize("@ss.hasPermi('domain:type:add')")
    @PostMapping
    public AjaxResult add(@RequestBody RentalType rentalType) {
        return toAjax(rentalTypeService.insertRentalType(rentalType));
    }

    /**
     * 修改租售类型
     */
    @PreAuthorize("@ss.hasPermi('domain:type:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody RentalType rentalType) {
        return toAjax(rentalTypeService.updateRentalType(rentalType));
    }

    /**
     * 删除租售类型
     */
    @PreAuthorize("@ss.hasPermi('domain:type:remove')")
    @DeleteMapping("/{rentalIds}")
    public AjaxResult remove(@PathVariable Long[] rentalIds) {
        return toAjax(rentalTypeService.deleteRentalTypeByRentalIds(rentalIds));
    }
}
